package ui;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class CustomerRegisterFrame extends JFrame {

	private JPanel contentPane;
	private JTextField idTxt;
	private JTextField addressTxt;
	private JTextField telNumberTxt;
	private JTextField passwordTxt;
	private JTextField passwordVerifiedTxt;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					CustomerRegisterFrame frame = new CustomerRegisterFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public CustomerRegisterFrame() {
		setTitle("注册");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 551, 418);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		JLabel lblNewLabel = new JLabel("ID：");
		lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 20));
		
		JLabel label = new JLabel("地    址：");
		
		JLabel label_1 = new JLabel("电    话：");
		
		JLabel label_2 = new JLabel("密    码：");
		
		JLabel label_3 = new JLabel("确认密码：");
		
		idTxt = new JTextField();
		idTxt.setColumns(10);
		
		addressTxt = new JTextField();
		addressTxt.setColumns(10);
		
		telNumberTxt = new JTextField();
		telNumberTxt.setColumns(10);
		
		passwordTxt = new JTextField();
		passwordTxt.setColumns(10);
		
		passwordVerifiedTxt = new JTextField();
		passwordVerifiedTxt.setColumns(10);
		
		JButton registerBtn = new JButton("注册");
		registerBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				registerActionPerformed(e);
			}
		});
		
		JButton resetValueBtn = new JButton("重置");
		resetValueBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				resetValueActionPerformed(e);
			}
		});
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(84)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
									.addComponent(lblNewLabel)
									.addComponent(label))
								.addComponent(label_1)
								.addComponent(label_2)
								.addComponent(label_3))
							.addGap(72)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addComponent(passwordTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(telNumberTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(idTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(passwordVerifiedTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(addressTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(121)
							.addComponent(registerBtn)
							.addGap(46)
							.addComponent(resetValueBtn)))
					.addContainerGap(167, Short.MAX_VALUE))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(59)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel)
						.addComponent(idTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(label)
						.addComponent(addressTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addComponent(label_1)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(label_2)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
								.addComponent(label_3)
								.addComponent(passwordVerifiedTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addComponent(telNumberTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(passwordTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
					.addPreferredGap(ComponentPlacement.RELATED, 35, Short.MAX_VALUE)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(registerBtn)
						.addComponent(resetValueBtn))
					.addGap(40))
		);
		contentPane.setLayout(gl_contentPane);
	}
	
	/**
	 * 注册按钮具体实现
	 */
    private void registerActionPerformed(ActionEvent e) {
        int id = -1;//初始化一下，以便等会儿判空
        String temp = this.idTxt.getText();
        if(!temp.isEmpty()) {
            id = Integer.parseInt(this.idTxt.getText());
        }
//        System.out.println(id);
        String address = this.addressTxt.getText();
        String telNumber = this.telNumberTxt.getText();
        String password = this.passwordTxt.getText();
        String passwordVerfied = this.passwordVerifiedTxt.getText();
        if(id == -1) {
			JOptionPane.showMessageDialog(null, "ID不能为空！");
			return;
        }
        if(address.isEmpty()) {
            JOptionPane.showMessageDialog(null, "地址不能为空！");
            return;
        }
        if(telNumber.isEmpty()) {
            JOptionPane.showMessageDialog(null, "电话号码不能为空！");
            return;
        }
        if(password.isEmpty()) {
            JOptionPane.showMessageDialog(null, "密码不能为空！");
            return;
        }
        if(passwordVerfied.isEmpty()) {
            JOptionPane.showMessageDialog(null, "请输入确认密码！");
            return;
        }
        if(!password.equals(passwordVerfied)) {
            JOptionPane.showMessageDialog(null, "两次输入的密码不一致！");
            return;
        }
    }
	/**
	 * 重置按钮具体实现
	 * @param e
	 */
    private void resetValueActionPerformed(ActionEvent e) {
        this.idTxt.setText("");
        this.passwordTxt.setText("");
        this.passwordVerifiedTxt.setText("");
        this.addressTxt.setText("");
        this.telNumberTxt.setText("");
    };
}
